package com.example.big_orange2.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.notice;
import com.example.big_orange2.entity.pagedata_n;
import com.example.big_orange2.mapper.noticeMapper;
import com.example.big_orange2.service.NoticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class noticeController {
    @Resource
    noticeMapper noticeMapper;
    @Resource
    NoticeService noticeService;
    //增
    //向数据库写入（insert）数据的接口
    @PostMapping("/add")
    public Result<?> save(@RequestBody notice notice){//?是泛型，表示任意类型
        //设置默认值
        //插入的操作
        noticeMapper.insert(notice);
        return Result.success();//返回操作情况
    }
    //向数据库删除（delete）数据的接口
    @DeleteMapping("/delete/{noticeid}")//占位
    public Result<?> delete(@PathVariable String noticeid){//?是泛型，表示任意类型
        //删除的操作
        noticeMapper.deleteById(noticeid);
        return Result.success();//返回操作情况

    }
    //修改功能
    @PutMapping("/update")
    //向数据库更新（update）数据的接口
    public Result<?> update(@RequestBody notice notice){//?是泛型，表示任意类型
        noticeMapper.updateById(notice);
        return Result.success();
    }

    //全部查询
    @GetMapping("/getdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> finduser(){
        LambdaQueryWrapper<notice> wrapper = Wrappers.<notice>lambdaQuery();
        wrapper.orderByDesc(notice::getNoticetime);
        List<notice> Loanhandle = noticeMapper.selectList(wrapper);//

        return  Result.success(Loanhandle);//返回结果

    }

    @GetMapping("/getdatanoticeSort")
    public Result<?> findnoticeSort(){
        LambdaQueryWrapper<notice> wrapper = Wrappers.<notice>lambdaQuery();
        wrapper.orderByDesc(notice::getNoticetime);
        List<notice> Loanhandle = noticeMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果
    }

    //依据用户id查询
    @GetMapping("/getdata/{noticeid}")
    //根据员工号，挑选他负责的客户信息
    public Result<?> findoneuser(@PathVariable String noticeid){
        LambdaQueryWrapper<notice> wrapper =Wrappers.<notice>lambdaQuery();
        wrapper.eq(notice::getNoticeid,noticeid);//多条件查询
        List<notice> Loanhandle = noticeMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }

    @PostMapping("/paging/{page}/{limit}")
    //分页查询(select),查询数据接口
    public Result<?> findPage(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              @RequestBody(required = false)  String s
    ){
        Result<?> result=new Result<>();
        result=noticeService.findPage(page,limit,s);
        return  result;

    }
}
